package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.ConstantItem;
import his.entity.ConstantType;
import his.persistence.ConstantItemMapper;
import his.persistence.ConstantTypeMapper;
import his.service.ConstantService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConstantServiceImpl implements ConstantService {

    @Autowired
    private ConstantTypeMapper constantTypeMapper;

    @Autowired
    private ConstantItemMapper constantItemMapper;

    @Override
    public CommonResponse<List<ConstantType>> getall_consttypes() {
        QueryWrapper<ConstantType> queryWrapper=new QueryWrapper<>();
        List<ConstantType> constantTypeList=constantTypeMapper.selectList(queryWrapper);
        if(constantTypeList.size()>0)
            return CommonResponse.createForSuccess(constantTypeList);
        else
            return CommonResponse.createForError("查找失败");
    }

    @Override
    public CommonResponse<List<ConstantType>> get_consttypes_bycode(String keywords) {
        QueryWrapper<ConstantType> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ConstantTypeCode",keywords);
        List<ConstantType> constantTypeList1=constantTypeMapper.selectList(queryWrapper1);
        if(constantTypeList1.size()>0)
            return CommonResponse.createForSuccess(constantTypeList1);
        else{
            QueryWrapper<ConstantType> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ConstantTypeName",keywords);
            List<ConstantType> constantTypeList2=constantTypeMapper.selectList(queryWrapper2);
            if(constantTypeList2.size()>0){
                return CommonResponse.createForSuccess(constantTypeList2);

            }else{
                return CommonResponse.createForSuccessMessage("未找到对应的常数类别");
            }
        }


    }

    @Override
    public CommonResponse<List<ConstantItem>> getall_constitems(){
        QueryWrapper<ConstantItem> queryWrapper=new QueryWrapper<>();
        List<ConstantItem> constantItemList=constantItemMapper.selectList(queryWrapper);
        if(constantItemList.size()>0)
            return CommonResponse.createForSuccess(constantItemList);
        else
            return CommonResponse.createForError("查找失败");
    }

    @Override
    public CommonResponse<List<ConstantItem>> get_constitems_byidname(String keywords) {
        QueryWrapper<ConstantItem> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("ConstantTypeID",keywords);
        List<ConstantItem> constantItemList1=constantItemMapper.selectList(queryWrapper1);
        if(constantItemList1.size()>0){
            return CommonResponse.createForSuccess(constantItemList1);
        }else{
            QueryWrapper<ConstantItem> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("ConstantCode",keywords);
            List<ConstantItem> constantItemList2=constantItemMapper.selectList(queryWrapper2);
            if(constantItemList2.size()>0){
                return CommonResponse.createForSuccess(constantItemList2);
            }else{
                QueryWrapper<ConstantItem> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("ConstantName",keywords);
                List<ConstantItem> constantItemList3=constantItemMapper.selectList(queryWrapper3);
                if(constantItemList3.size()>0){
                    return CommonResponse.createForSuccess(constantItemList3);
                }else{
                    return CommonResponse.createForSuccessMessage("未找到对应的常数项");
                }
            }
        }

    }
}
